package kodkod.analysis;

import java.util.HashMap;
import java.util.Map;

public class CountingHashMap<K> extends HashMap<K,Integer> {

	private static final long serialVersionUID = -5225084305767204979L;

	public CountingHashMap() {}

	public CountingHashMap(int initialCapacity) {
		super(initialCapacity);
	}

	public CountingHashMap(Map<K,Integer> m) {
		super(m);
	}

	public CountingHashMap(int initialCapacity, float loadFactor) {
		super(initialCapacity, loadFactor);
	}
	
	public boolean inc(K key) {
		Integer c = get(key);
		if (c == null) {
			put(key, 1);
			return false;
		} else {
			put(key, c + 1);
			return true;
		}
	}
	
	public boolean dec(K key) {
		Integer c = get(key);
		if (c == null) {
			put(key, -1);
			return false;
		} else {
			put(key, c - 1);
			return true;
		}
	}
	
	public Integer get(K key) {
		Integer c = super.get(key);
		if (c == null) {
			return 0;
		} else {
			return c;
		}
	}

}
